package com.caration.shell;

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Handler;

import com.caration.shell.DatabaseStruct.Favorites;
import com.caration.shell.settings.SettingBroadcastReceiver;

public class ShellApplication extends Application {
	public static ShellModel mModel;
	private final ContentObserver mObserver = new FavoritesContentObserver(
			this, new Handler());

	static ShellModel getModel() {
		return mModel;
	}

	static ShellModel a(Home home) {
		mModel.a(home);
		return mModel;
	}

	public void onCreate() {
		// VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024);
		super.onCreate();
		mModel = new ShellModel(this);

		IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
		filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
		filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
		filter.addDataScheme("package");
		registerReceiver(mModel, filter);

		filter = new IntentFilter();
		filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
		filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
		registerReceiver(mModel, filter);

		filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
		filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
		registerReceiver(new SettingBroadcastReceiver(), filter);

		filter = new IntentFilter();
		filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
		registerReceiver(ShellWallpaperManager.instance(), filter);

		filter = new IntentFilter();
		filter.addAction(InstallShortcutReceiver.ACTION_INSTALL_SHORTCUT);
		registerReceiver(InstallShortcutReceiver.instance(), filter);

		getContentResolver().registerContentObserver(Favorites.CONTENT_URI,
				true, mObserver);
	}

	public void onTerminate() {
		super.onTerminate();
		unregisterReceiver(mModel);
		getContentResolver().unregisterContentObserver(mObserver);
	}
}
